package com.woyou.androidsample.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.woyou.androidsample.Constant;
import com.woyou.androidsample.R;
import com.woyou.androidsample.bean.FamousInfo;
import com.woyou.androidsample.bean.FamousInfoReq;
import com.woyou.androidsample.model.FamousInfoModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者： andli
 * 时间： 2017/11/23 16:03
 * 描述： 首页
 */
public class MainActivity extends AppCompatActivity {

    private FamousInfoModel famousInfoModel;

    private EditText mEditKeyWord;
    private ImageButton mSerachBtn;
    private TextView mTxtContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        famousInfoModel = FamousInfoModel.getInstance(getApplicationContext());

        initView();
        initParams();
        initEvent();
    }

    /**
     * 初始化View
     */
    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mEditKeyWord = (EditText) findViewById(R.id.edit_keyword);
        mSerachBtn = (ImageButton) findViewById(R.id.button_search);
        mTxtContent = (TextView) findViewById(R.id.txt_content);

    }

    /**
     * 初始化参数
     */
    private FamousInfoReq initParams() {
        FamousInfoReq mFamousInfoReq = null;
        mFamousInfoReq = new FamousInfoReq();
        mFamousInfoReq.apiKey = Constant.APIKEY;
        mFamousInfoReq.keyword = mEditKeyWord.getText().toString();
        mFamousInfoReq.page = 3;
        mFamousInfoReq.rows = 30;
        return mFamousInfoReq;

    }

    // 获取事件
    private void initEvent() {

        mSerachBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 创建访问的API请求
                Call<FamousInfo> callFamous = famousInfoModel.queryLookUp(initParams());

                // 发送请求
                callFamous.enqueue(new Callback<FamousInfo>() {

                    @Override
                    public void onResponse(Call<FamousInfo> call, Response<FamousInfo> response) {
                        if (response.isSuccess()) {

                            // 需要判断返回的数据是否正确，否则闪退

                            FamousInfo result = response.body();
                            if (result != null) {
                                List<FamousInfo.ResultEntity> entity = result.getResult();
                                mTxtContent.setText("1、" + entity.get(0).getFamous_saying() + "\n---" + entity.get(0).getFamous_name() + "\n 2、" + entity.get(1).getFamous_saying() + "\n---" + entity.get(1).getFamous_name());
                            }

                        }
                    }

                    @Override
                    public void onFailure(Call<FamousInfo> call, Throwable t) {

                    }
                });

            }
        });
    }
}
